package com.sgj.controller.admin;

import com.sgj.pojo.Type;
import com.sgj.service.TypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.validation.Valid;
import java.util.Objects;


/**
 * @author 沈国纪
 * @date 2021/3/30 17:29
 */
@Controller
@RequestMapping("/admin")
public class TypeController {

	private static final String TYPES_INPUT = "admin/types-input";
	private static final String TYPES = "admin/types";
	private static final String REDIRECT_TYPES = "redirect:/admin/types";

	@Autowired
	private TypeService typeService;

	/**
	 * 分页查询
	 * @param pageable
	 * @param model
	 * @return
	 */
	@GetMapping("/types")
	public String types(@PageableDefault(size = 10, sort = {"id"}, direction = Sort.Direction.DESC) Pageable pageable,
	                    Model model) {
		model.addAttribute("page", typeService.listType(pageable));
		return TYPES;
	}

	/**
	 * 跳转到新增页面
	 * @param model
	 * @return
	 */
	@GetMapping("/types/input")
	public String input(Model model) {
		model.addAttribute("type", new Type());
		return TYPES_INPUT;
	}

	/**
	 * 修改分类页面跳转
	 * @param id
	 * @param model
	 * @return
	 */
	@GetMapping("/types/{id}/input")
	public String updateInput(@PathVariable Long id, Model model) {
		model.addAttribute("type", typeService.findTypeById(id));
		return TYPES_INPUT;
	}

	/**
	 * 删除分类
	 * @param id
	 * @param attributes
	 * @return
	 */
	@GetMapping("/types/{id}/delete")
	public String deleteInput(@PathVariable Long id, RedirectAttributes attributes) {
		typeService.deleteType(id);
		attributes.addFlashAttribute("message", "删除成功");
		return REDIRECT_TYPES;
	}

	/**
	 * 新增分类
	 * @param type
	 * @return
	 */
	@PostMapping("/types")
	public String save(@Valid Type type, BindingResult result, RedirectAttributes attributes) {
		Type type1 = typeService.findTypeByName(type.getName());
		if (Objects.nonNull(type1)) {
			result.rejectValue("name", "nameError", "不能添加重复分类");
		}
		// 判断校验是否通过
		if (result.hasErrors()) {
			return TYPES_INPUT;
		}
		Type t = typeService.saveType(type);
		if (Objects.isNull(t)) {
			attributes.addFlashAttribute("message", "添加失败");
		} else {
			attributes.addFlashAttribute("message", "添加成功");
		}
		return REDIRECT_TYPES;
	}

	/**
	 * 更新分类
	 * @param type
	 * @param result
	 * @param id
	 * @param attributes
	 * @return
	 */
	@PostMapping("/types/{id}")
	public String update(@Valid Type type, BindingResult result, @PathVariable Long id, RedirectAttributes attributes) {
		Type type1 = typeService.findTypeByName(type.getName());
		if (Objects.nonNull(type1)) {
			result.rejectValue("name", "nameError", "不能添加重复分类");
		}
		// 判断校验是否通过
		if (result.hasErrors()) {
			return TYPES_INPUT;
		}
		Type t = typeService.updateType(type);
		if (Objects.isNull(t)) {
			attributes.addFlashAttribute("message", "更新失败");
		} else {
			attributes.addFlashAttribute("message", "更新成功");
		}
		return REDIRECT_TYPES;
	}
}
